package com.jbookshelf.dal.providers;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.jbookshelf.dal.entities.Languages;
import com.jbookshelf.dal.entities.Shops;

@Repository
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public class ShopsDataProviderImpl implements ShopsDataProvider {

	@Autowired
	SessionFactory sessionFactory;

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public List<Shops> getShops() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Shops");
		return query.list();
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void insertShop(Shops shop) {
		Session s = sessionFactory.getCurrentSession();
		s.save(shop);

	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public Shops getShopByName(String name) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Shops where name = :name").setString("name", name);
		return (Shops)query.uniqueResult();
	}

}
